<?php

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

// Require specific controller
// Controller

//load base classes
require_once (JPATH_COMPONENT.DS.'base'.DS.'loader.php');

//defines CKEDITOR library includes path
define('CKEDITOR_LIBRARY',JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'includes'.DS.'ckeditor'); 


define('JCK_COMPONENT', JUri::root() . 'administrator/components/com_jckman');

//load  default style sheets
$document = JFactory::getDocument();
$document->addStyleSheet( JCK_COMPONENT . '/css/header.css', 'text/css' );


jckimport('base.controller');

//register all event listeners
JCKRegisterAllEventlisetners();


if(is_dir(CKEDITOR_LIBRARY))
{
	$controller = JRequest::getWord('controller','cpanel');
	if($controller == 'cpanel')
	{
		
		if(JRequest::getCmd('task','') != 'check')
		{
			$document->addStyleSheet( JCK_COMPONENT . '/css/message.css', 'text/css' );
			$mainframe =& JFactory::getApplication();	
			$mainframe->enqueueMessage("This is a free version of the JCK manger with limited functionality. If you require plugin management features please upgrade to the professional version.","notice");
		}
	}
}	
else
{	
	$controller = 'cpanel';
	$mainframe =& JFactory::getApplication();	
	$mainframe->enqueueMessage("System couldn't detect JoomlaCK Edtior: Please check file permissions or ensure you have installed the editor");
}	
// Require specific controller if requested


jckimport('controllers.' . $controller );

//require_once (JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php');

if($controller == "Install")
{

	require_once (JPATH_COMPONENT.DS.'controllers'.DS. 'install.php');
 
   $ext = JRequest::getWord('type');
   $subMenus = array(	'Plugins'	=> 'plugin'   						);
	addCustomSubMenus($controller,$ext,$subMenus,'manage','Post');
    // Create the controller
	
	$controller = new InstallerController(array(
	'default_task' => 'installform','base_path' =>  dirname( __FILE__ )));
	 	
	$controller->execute(JRequest::getCmd( 'task' ));
	$controller->redirect();

}
else 
{
    // main helper class
	jckimport('helper');
	// global include classes
	jckimport('parameter.parameter');
	jckimport('html.html');
	
	$ext = JRequest::getWord('type');
    $subMenus = array(	'Plugin Manager'=> 'list','Installer'=> 'Install');
		
	
	addCustomSubMenus($controller,$ext,$subMenus,'');
	
	
	// Create the controller
	$classname    = $controller. 'Controller';
	$controller   = new $classname( );
	$controller->execute( JRequest::getCmd( 'task' ) );
	$controller->redirect();
}

function addCustomSubMenus($controller,$type, $subMenus, $task = '',$action = 'Get')
{

	switch($action)
	{
		case 'Post' :
			JSubMenuHelper::addEntry(JText::_( $controller ), '#" onclick="javascript:document.adminForm.type.value=\''.$controller.'\';document.adminForm.task.value=\'\';submitbutton(\'\');', !in_array( $type, $subMenus));
		
			foreach ($subMenus as $name => $extension) 
			{
				JSubMenuHelper::addEntry(JText::_( $name ), '#" onclick="javascript:document.adminForm.type.value=\''.$extension.'\';submitbutton(\''. $task.'\');', (			$extension == $type));
			}
		
		break;
		default :
		
			if($type == '')
				$type = $controller;
			foreach ($subMenus as $name => $extension) 
			{
				JSubMenuHelper::addEntry(JText::_( $name ), 'index.php?option=com_jckman&controller='.$extension.'"', ($extension == $type));
			}
	}

 	
}

?>


